package com.yzt.base.kit;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("返回值包装类")
public class BaseResult<T> {
    public static final int CODE_ERROR = 1;
    public static final int CODE_SUCCESS = 0;

    @ApiModelProperty("状态代码")
    private int code;

    @ApiModelProperty("返回消息")
    private String msg;

    @ApiModelProperty("返回数据")
    private T data;

    public BaseResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public BaseResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public BaseResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static <T> BaseResultBuilder<T> builder() {
        return new BaseResultBuilder<>();
    }

    public static class BaseResultBuilder<T> {
        private int code;

        private String msg;

        private T data;

        public BaseResultBuilder<T> code(int code) {
            this.code = code;
            return this;
        }

        public BaseResultBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public BaseResultBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public BaseResult<T> build() {
            return new BaseResult<>(this.code, this.msg, this.data);
        }

        @Override
        public String toString() {
            return "BaseResultBuilder [code=" + code + ", msg=" + msg + ", data=" + data + "]";
        }
    }

    public BaseResult() {
    }

    public BaseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public static <E> BaseResult<E> ok(int code, String msg) {
        return new BaseResult<>(code, msg, null);
    }

    public static <E> BaseResult<E> ok(String msg, E data) {
        return new BaseResult<>(CODE_SUCCESS, msg, data);
    }

    public static <E> BaseResult<E> okData(E data) {
        return ok("成功", data);
    }

    public static <E> BaseResult<E> ok(String msg) {
        return ok(msg, (E) null);
    }

    public static <E> BaseResult<E> ok() {
        return okData(null);
    }

    public static <E> BaseResult<E> fail(int code, String msg) {
        return new BaseResult<>(code, msg, null);
    }

    public static <E> BaseResult<E> fail(String msg, E data) {
        return new BaseResult<>(CODE_ERROR, msg, data);
    }

    public static <E> BaseResult<E> failData(E data) {
        return fail("系统繁忙", data);
    }

    public static <E> BaseResult<E> fail(String msg) {
        return fail(msg, (E) null);
    }

    public static <E> BaseResult<E> fail() {
        return failData(null);
    }

    @Override
    public String toString() {
        return "BaseResult [code=" + code + ", msg=" + msg + ", data=" + data + "]";
    }
}
